import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_li_ke_jie/entity/app_product.dart';
import 'package:flutter_li_ke_jie/ui/product/product_mode.dart';

import '../../core/interface/common_interface.dart';
import '../../style/common_color.dart';
import '../../utils/img_util.dart';
import '../../utils/navigator_util.dart';
import '../../utils/sputil.dart';

/// 产品列表页
class ProductPage extends StatefulWidget {
  const ProductPage({Key? key}) : super(key: key);

  @override
  _ProductPageState createState() => _ProductPageState();
}

class _ProductPageState extends State<ProductPage> with CommonInterface<ProductModel>{
  late ScrollController _controller; // 执行动画的controller

  late ProductModel _productModel;
  bool _isLoadData = false;//是否已经加载数据
  bool _isVip = false;//是否bip
  @override
  void initState() {
    _controller = ScrollController();
    _isLoadData = false;
    int? isvIP = SPUtil.getVipState();

    if(isvIP!=null&&isvIP==1){
      setState(() {
        _isVip = true;
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _productModel = model(context);
    if(!_isLoadData){
      _productModel.getProductList();
      _isLoadData = true;
    }
    return Scaffold(
        appBar: _appBar(),
        body: RefreshIndicator(
          child:Container(
            // width: double.infinity,
            // height: double.infinity,
            padding: EdgeInsets.only(bottom: 20),
            child: Column(
              children: [
                Expanded(child:  ListView(
                  children: [
                    Column(
                      children: [
                        Image.asset( _isVip?ImageUtil.loadLocalImg("product_top2.png"):ImageUtil.loadLocalImg("product_top1.png"),height: 200,fit: BoxFit.fill,width: MediaQuery.of(context).size.width,),
                        Container(
                          width: double.infinity,
                          // height:  _productModel.products.length*180,
                          margin: const EdgeInsets.only(left: 10,right: 10,top: 10),
                          child: ListView.builder(
                            physics: const NeverScrollableScrollPhysics(),
                            shrinkWrap: true,//使用子控件的总长度来设置ListView的长度（这里的长度为高度）

                            controller: _controller, // 滚动的controller
                            itemCount: _productModel.products.length,
                            itemBuilder: (context, index) {
                              AppProduct appProduct = _productModel.products.elementAt(index);
                              return appProduct.isVip==1?_productVipItem(appProduct):_productItem(appProduct);
                            },
                          ),
                        )
                      ],
                    )

                  ],
                ),flex: 1,)
              ],
            )
          ),
          onRefresh: _onRefresh,
        )
    );
  }

  //刷新
  Future<void> _onRefresh() async {
    //查询首页产品
    _productModel.getProductList();
  }

  //标题
  AppBar _appBar() {
    return AppBar(
      backgroundColor: color_f7,
      elevation: 0,
      centerTitle: true,
      leading: IconButton(//左边按钮
        icon: Icon(Icons.arrow_back,color: color_333,),
        onPressed: (){
          NavigatorUtil.pop(context);
        },
      ),
      title: Text("产品列表", style: TextStyle(
          color: color_1e, fontSize: 20, fontWeight: FontWeight.bold),),
    );
  }

  ///普通item
  Widget _productItem(AppProduct appProduct) {
    return  Container(
      margin: EdgeInsets.only(bottom: 10,right: 5),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10))),
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          //头部
          Row(
            children: [
              Image.network(
                appProduct.logoUrl,
                height: 36,
                width: 36,
                fit:BoxFit.fill ,
              ),
              const SizedBox(
                width: 10,
              ),
              Text(
                "${appProduct.name}",
                style: TextStyle(
                    color: color_1e,
                    fontSize: 16,
                    fontWeight: FontWeight.bold),
              ),
              const SizedBox(
                width: 10,
              ),
              //标签1
              _labelWidget("安全快捷", color_e0f, color_3775F6),
              SizedBox(
                width: 5,
              ),
              //标签2
              _labelWidget("下款率15%", color_FFF7E8, color_D08D18),
            ],
          ),
          SizedBox(height: 10,),
          //内容
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              //左边金额
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("${appProduct.maxPrice}",style: TextStyle(color: color_fa2,fontSize: 28,fontWeight:FontWeight.bold),),
                  SizedBox(height: 2,),
                  Text("最高可借款额度(元)",style: TextStyle(color: color_999,fontSize: 14),),
                ],
              ),
              SizedBox(width: 10,),
              //中间标签
              Column(
                children: [
                  Text("日利率${appProduct.dayRate}",style: TextStyle(color: color_999,fontSize: 14),),
                  SizedBox(height: 2,),
                  Text("期限3-36月",style: TextStyle(color: color_999,fontSize: 14),),
                ],
              ),
              SizedBox(width: 20,),
              //左边按钮
              Expanded(
                child: Container(
                  decoration: const BoxDecoration(
                      color: Color(0xFF3775F6),
                      borderRadius: BorderRadius.all(Radius.circular(10))
                  ),
                  child: MaterialButton(
                    onPressed: (){

                    },
                    child: const Text("去申请",style: TextStyle(color: Colors.white,fontSize: 18),),
                  ),
                ),
                flex: 1,
              )
            ],
          ),
          SizedBox(height: 10,),
          //底部
          Container(

            padding: EdgeInsets.only(left: 10,right: 10,top: 5,bottom: 5),
            decoration: BoxDecoration(
                color: color_F4F7FE,
                borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              children: [
                Row(
                  children: [
                    Image.asset(ImageUtil.loadLocalImg("loan_correct_blue.png"),height: 20,width: 20,),
                    SizedBox(width: 5,),
                    Text("方便快捷",style: TextStyle(color: color_3775F6,fontSize: 14),)
                  ],
                ),
                SizedBox(width: 30,),
                Expanded(
                  child:Row(
                    children: [
                      Image.asset(ImageUtil.loadLocalImg("loan_correct_blue.png"),height: 20,width: 20,),
                      SizedBox(width: 5,),
                      Text("最后一笔下款时间:${ Random().nextInt(10)+1}分钟前",style: const TextStyle(color: color_3775F6,fontSize: 14),)
                    ],
                  ),
                  flex: 1,
                ),
              ],
            ),
          )
        ],
      ),
    );
  }


  ///vip产品item
  Widget _productVipItem(AppProduct appProduct) {
    return Stack(
      children: [
        Container(
          margin: EdgeInsets.only(bottom: 10,right: 5),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))),
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              //头部
              Row(
                children: [
                  Image.network(
                    appProduct.logoUrl,
                    height: 36,
                    width: 36,
                    fit:BoxFit.fill ,
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  Text(
                    "${appProduct.name}",
                    style: TextStyle(
                        color: color_1e,
                        fontSize: 16,
                        fontWeight: FontWeight.bold),
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  //标签1
                  _labelWidget("安全快捷", color_e0f, color_3775F6),
                  SizedBox(
                    width: 5,
                  ),
                  //标签2
                  _labelWidget("下款率15%", color_FFF7E8, color_D08D18),
                  //图标
                  // Expanded(
                  //   child: Container(
                  //     alignment: Alignment.centerRight,
                  //     child: Image.asset(
                  //       ImageUtil.loadLocalImg("loan_vip.png"),
                  //       height: 20,
                  //     ),
                  //   ),
                  //   flex: 1,
                  // )
                ],
              ),
              SizedBox(height: 10,),
              //内容
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  //左边金额
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text("${appProduct.maxPrice}",style: TextStyle(color: color_fa2,fontSize: 28,fontWeight:FontWeight.bold),),
                      SizedBox(height: 2,),
                      Text("最高可借款额度(元)",style: TextStyle(color: color_999,fontSize: 14),),
                    ],
                  ),
                  SizedBox(width: 10,),
                  //中间标签
                  Column(
                    children: [
                      Text("日利率${appProduct.dayRate}",style: TextStyle(color: color_999,fontSize: 14),),
                      SizedBox(height: 2,),
                      Text("期限3-36月",style: TextStyle(color: color_999,fontSize: 14),),
                    ],
                  ),
                  SizedBox(width: 20,),
                  //左边按钮
                  Expanded(
                    child: Container(
                      decoration: const BoxDecoration(
                          gradient: LinearGradient(colors: [
                            Color(0xFFF4D0A1),
                            Color(0xFFFFA200)
                          ]),
                          borderRadius: BorderRadius.all(Radius.circular(10))
                      ),
                      child: MaterialButton(
                        onPressed: (){

                        },
                        child: const Text("去申请",style: TextStyle(color: color_774418,fontSize: 18),),
                      ),
                    ),
                    flex: 1,
                  )
                ],
              ),
              SizedBox(height: 10,),
              //底部
              Container(

                padding: EdgeInsets.only(left: 10,right: 10,top: 5,bottom: 5),
                decoration: BoxDecoration(
                    color: color_FFF9ED,
                    borderRadius: BorderRadius.all(Radius.circular(10))
                ),
                child: Row(
                  children: [
                    Row(
                      children: [
                        Image.asset(ImageUtil.loadLocalImg("loan_correct_yellow.png"),height: 20,width: 20,),
                        SizedBox(width: 5,),
                        Text("方便快捷",style: TextStyle(color: color_FFA200,fontSize: 14),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(
                      child:Row(
                        children: [
                          Image.asset(ImageUtil.loadLocalImg("loan_correct_yellow.png"),height: 20,width: 20,),
                          SizedBox(width: 5,),
                          Text("最后一笔下款时间:${ Random().nextInt(10)+1}分钟前",style: TextStyle(color: color_FFA200,fontSize: 14),)
                        ],
                      ),
                      flex: 1,
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
        Positioned(
          child: Image.asset(
            ImageUtil.loadLocalImg("loan_vip.png"),
            height: 20,
          ),
          right: 0,
          top: 20,
        )
      ],
    );
  }

  //标签组件
  Widget _labelWidget(String label, Color bgColor, Color textColor) {
    return Container(
        padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3),
        decoration: BoxDecoration(
            color: bgColor,
            borderRadius: BorderRadius.all(Radius.circular(10))
        ),
        child: Center(
          child: Text(label, style: TextStyle(color: textColor, fontSize: 12),),
        )
    );
  }
}
